package com.platform.system.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.platform.common.bean.SystemConfigBean;
import com.platform.common.menus.SysConfigEnum;
import com.platform.system.entity.SysConfig;
import com.platform.system.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * describe:平台系统配置
 * 系统启动时加载
 * 用于控制权限
 * @author Administrator
 * @date 2020/04/30
 * @Time 0:10
 */
@Slf4j
@Component
public class SystemConfig {

    @Resource
    private SysConfigService sysConfigService;

    private SystemConfig(){
    }

    /**
     * 系统启动时加载
     */
    @PostConstruct
    public void init(){
        SysConfig sysConfig = sysConfigService.getOne(
                new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getCode, SysConfigEnum.Code.getValue()));
        if (null == sysConfig){
            log.error("初始化平台系统配置错误，请检查是否sys_config表数据是否异常");
            throw new RuntimeException();
        }
        SystemConfigBean.setConfig(
                SysConfigEnum.RoleInheritance.getEnumByValue(sysConfig.getRoleInheritance()) ,
                SysConfigEnum.PermissionGrain.getEnumByValue(sysConfig.getPermissionGrain()) ,
                SysConfigEnum.SeparationOfDuty.getEnumByValue(sysConfig.getSeparationOfDuty())
        );
    }
}
